package com.cbs.android.widget.tipview.sample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cbs.android.widget.tipview.Adapter;
import com.cbs.android.widget.tipview.OnClickListener;
import com.cbs.android.widget.tipview.TipView;
import com.cbs.android.widget.tipview.ViewHolder;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getName();
    private RecyclerView recyclerView;
    private MyAdapter adapter = new MyAdapter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TipView tipView = (TipView) findViewById(R.id.atipview);
        tipView.setAdapter(adapter);
        tipView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v, int index) {
                Log.e(TAG, index + "");
            }
        });

        recyclerView = (RecyclerView) findViewById(R.id.list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new MyLAdapter());
    }

    public void load(View view) {
        adapter.a = adapter.a + 10;
        if (adapter.count > 1) {
            adapter.count = adapter.count - 1;
        } else {
            adapter.count = adapter.count + 1;
        }

        adapter.notifyDataSetChanged();
    }

    private class MyLAdapter extends RecyclerView.Adapter {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = getLayoutInflater().inflate(R.layout.litem, parent, false);
            MyLAdapter.MyHolder myHolder = new MyLAdapter.MyHolder(view);
            return myHolder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        }

        @Override
        public int getItemCount() {
            return 20;
        }

        class MyHolder extends RecyclerView.ViewHolder {
            public TipView tipView;

            public MyHolder(View itemView) {
                super(itemView);
                tipView = (TipView) itemView.findViewById(R.id.tip_view);
                tipView.setAdapter(adapter);
            }
        }
    }

    private class MyAdapter extends Adapter {
        public int a = 0;
        public int count = 3;

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == 0) {
                View view = getLayoutInflater().inflate(R.layout.item, parent, false);
                ViewHolder viewHolder = new ViewHolder(view);
                return viewHolder;
            } else {
                View view = getLayoutInflater().inflate(R.layout.item2, parent, false);
                ViewHolder viewHolder = new ViewHolder(view);
                return viewHolder;
            }
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            TextView textView = (TextView) holder.itemView.findViewById(R.id.list_item);
            textView.setText(a++ + "");
        }

        @Override
        public int getItemViewType(int position) {
            return position % 2;
        }

        @Override
        public int getItemCount() {
            return count;
        }
    }
}
